package com.hoopchina.ui.main;

import java.util.List;

import com.hoopchina.news.R;
import com.hoopchina.data.ListBaseAdapter;
import com.hoopchina.meta.NewsItem;
import com.hoopchina.ui.home.HomeView;
import com.hoopchina.util.UrlUtils;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.Toast;



/**
 * NewsListAdapter
 * 新闻列表的适配器
 * 
 * @author Neusoft
 *
 */
public class NewsListAdapter extends ListBaseAdapter {
	private RelativeLayout	mLoadMoreButtonLayout;
	private LayoutInflater	mInflater;
	private boolean			mIsDispLoadMoreButton	= true;
	private Button			mLoadMoreButton;
	private ProgressBar		mLoadMoreButtonProgressBar;
	private ViewManager     mViewManager;
	private SharedPreferences mSettings;  
	private static final String		PREFS_NAME	= "MyPrefsFile";
	private boolean mIsNewsList;
	/**
	 * 设置显示更多的进度条的状态
	 * @param v
	 */
	public void setLoadMoreProgressBarVisibility(int v) {
		mLoadMoreButtonProgressBar.setVisibility(v);
	}
	/**
	 * 显示加载更多的按钮
	 * @param isDisp
	 */
    public void displayLoadMoreButton( boolean isDisp ) {
        mIsDispLoadMoreButton  = isDisp;
    }
    
    
	public NewsListAdapter(Context aContext, List<?> aData,
			final INewsListViewObserver loadMoreObserver, boolean IsNewsList) {
		super(aContext, aData);
		mIsNewsList = IsNewsList;
		mSettings = aContext.getSharedPreferences(PREFS_NAME, 0);
		mViewManager=ViewManager.getInstance();
		mResource = R.layout.newslistitem;
		mInflater = LayoutInflater.from(mContext);

		mLoadMoreButtonLayout = (RelativeLayout) mInflater.inflate(
				R.layout.loadmorebutton, null);
		mLoadMoreButton = (Button) mLoadMoreButtonLayout
				.findViewById(R.id.buttonLoadMore);

		mLoadMoreButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				loadMoreObserver.onClickLoadMoreButton();
			}
		});
		mLoadMoreButtonProgressBar = (ProgressBar) mLoadMoreButtonLayout
				.findViewById(R.id.progressBarLoadMore);

		// mNextTwentyButtonLayout = ( ListItemView ) mInflater.inflate(
		// R.layout.next_twenty_video, null );
	}
	/*
	 * 设置加载更多按钮文字
	 */
    public  void setLoadMoreButtonText(int aText){
    	mLoadMoreButton.setText(aText);
    }
    
    public   void setLoadMoreButtonVisibility(int isVisiblble){
    	mLoadMoreButtonLayout.setVisibility(isVisiblble);
    }
    /*
	 * 设置加载更多按钮是否可用
	 */
    public  void setLoadMoreButtonEnable(boolean isEnable){
    	mLoadMoreButton.setEnabled(isEnable);
    }

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
         //设置增加更多的显隐
		if (mData.size() <= position) {
			if (mIsDispLoadMoreButton == false) {
				mLoadMoreButtonLayout.setVisibility(NewsListItemView.INVISIBLE);
			} else {
				mLoadMoreButtonLayout.setVisibility(NewsListItemView.VISIBLE);
			}
			return mLoadMoreButtonLayout;
		}

		return getNewsItemView(position, convertView);

	}
	
	/**
	 * 对列表的条目view进行适配
	 * @param position 位置
	 * @param convertView 条目view
	 * @return 返回的view
	 */
	private RelativeLayout getNewsItemView(final int position, View convertView) {
		NewsItem item = (NewsItem) mData.get(position);
		if (item == null) {
			return (RelativeLayout) convertView;
		}

		NewsListItemView itemView = null;
		if (null == convertView || convertView == mLoadMoreButtonLayout
				|| convertView.findViewById(R.id.buttonLoadMore) != null) {
			itemView = new NewsListItemView(mContext);
		} else {
			itemView = (NewsListItemView) convertView;
		}
		
		if ( mIsNewsList ){
			String deitailMarker=UrlUtils.getNameFromUrl(item.getTitle().trim()+item.getTime().trim());
			//获得点击后颜色存储，默认是false，即没有颜色
			boolean textColor=mSettings.getBoolean(deitailMarker, false);
			if (textColor) {
				itemView.setTitleColor(Color.GRAY);
				itemView.setSummaryColor(Color.GRAY);
			} else {
				itemView.setTitleColor(mContext.getResources().getColorStateList(R.color.news_list_item_title_selector));
				itemView.setSummaryColor(mContext.getResources().getColorStateList(R.color.news_list_item_summary_selector));
			}
		}
		itemView.setTitle(item.getTitle());
		itemView.setSummary(item.getAbs());
		itemView.setFrom(item.getAuthor());
		if(mIsNewsList){
			if (HomeView.mCurrentWindow==UrlUtils.HOME_ITEMS[0]) {
				//焦点新闻用的发行时间字段
				itemView.setUpdateTime(item.getmPublicTime());
			}else {
				itemView.setUpdateTime(item.getTime());
			}
		}else{
			itemView.setUpdateTime(item.getTime());
		}
		itemView.setRawTime(item.getTime());
		itemView.setHaveImage(item.getImgUrl());

		return itemView;
	}
	/**
	 * 列表的item数
	 */
	public int getCount() {
		return mData.size() + 1;
	}
}
